package com.coolcode.common;

import com.coolcode.enums.ExceptionErrorCode;
import com.coolcode.exception.CriticalException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class ExceptionLogger {

    public static void log(Exception e) {
        String errCode = ExceptionErrorCode.SYSTEM_ERROR.getCode();
        String errMsg;
        String requestInfo = StringUtils.EMPTY;

        if (e instanceof CriticalException) {
            CriticalException criticalException = (CriticalException) e;
            errCode = criticalException.getErrorCode();
            errMsg = criticalException.getMessage();
            requestInfo = criticalException.getRequestInfo();
        } else {
            errMsg = e.getMessage();
        }

        List<String> fields = new ArrayList<>();
        fields.add(errCode);
        fields.add(errMsg);
        if (StringUtils.isNoneBlank(requestInfo)) {
            fields.add(requestInfo);
        }

        log.error(StringUtils.join(fields, "|"), e);
    }
}
